<div class="space-4"></div>
<div class="row">
	<form class="form-horizontal" role="form" id="templateAddForm">
		<div class="form-group">
			<label class="col-sm-2 control-label no-padding-right" for="form-field-1"><span style="font-size: 20px;">发送目标</span></label>
			<div class="col-sm-9">
				<input type="text" id="to" placeholder="请输入发送目标" name="to" value="">
			</div>
		</div>
		<div class="space-4"></div>
		<div class="form-group">
			<label class="col-sm-2 control-label no-padding-right" for="form-field-1"><span style="font-size: 20px;">模板编号</span></label>
			<div class="col-sm-4">
				<div class="input-group">
					<input class="form-control" type="text" id="templateId" placeholder="请输入模板编号"> <span class="input-group-btn"> <a class="btn btn-sm btn-default"
						href="$!{base}/temp/list"> <i class="icon-search bigger-110"></i> 查询模板
					</a>
					</span>
				</div>
			</div>
		</div>
		<div class="space-4"></div>
		<div class="form-group">
			<label class="col-sm-2 control-label no-padding-right" for="form-field-1"> <span style="font-size: 20px;">消息类型</span>
			</label>
			<div class="col-sm-4">
			<select class="form-control" id="type">
				<option>请选择消息类型</option>
				<optgroup label="--------------">
				    #foreach($type in ${obj.data.types})
				    	<option value="$!{type}">$!{type.description}</option>
				    #end
				</optgroup>
			</select>
			</div>
		</div>
		
		<div class="form-group">
			<label class="col-sm-2 control-label no-padding-right" for="form-field-1"> <span style="font-size: 20px;">推送平台</span>
			</label>
			<div class="col-sm-4">
			<select class="form-control" id="pushPlatform">
					<option>请选择推送平台</option>
					<optgroup label="--------------">
					     #foreach($pushPlatForm in ${obj.data.platforms})
				    		<option value="$!{pushPlatForm}">$!{pushPlatForm.description}</option>
				   		 #end
					</optgroup>
				</select>
			</div>
		</div>
		
		<div class="form-group">
			<label class="col-sm-2 control-label no-padding-right" for="form-field-1"> <span style="font-size: 20px;">推送方式</span>
			</label>
			<div class="col-sm-4">
			<select class="form-control" id="pushType">
					<option>请选择推送方式</option>
					<optgroup label="--------------">
					     #foreach($pushType in ${obj.data.pushTypes})
				    		<option value="$!{pushType}">$!{pushType.description}</option>
				   		 #end
					</optgroup>
				</select>
			</div>
		</div>
		
		<div class="form-group">
			<label class="col-sm-2 control-label no-padding-right" for="form-field-3"> <span style="font-size: 20px;">模板参数</span>
			</label>
			<div class="col-xs-8 col-sm-4">
				<table class="table table-striped table-bordered table-hover">
					<thead>
						<tr>
							<th>Key</th>
							<th>Value</th>
							<th></th>
						</tr>
					</thead>
					<tbody id="templateParas">
						<tr>
							<td><input type="text"></td>
							<td><input type="text"></td>
							<td><a class="btn btn-sm btn-danger" onclick="deleteParas(this)"><i class="icon-trash"></i>删除</a></td>
						</tr>
					</tbody>
					<tfoot>
						<tr>
							<td colspan="3"><button class="btn btn-sm btn-primary pull-right" onclick="addTemplateParas();" type="button">
									<i class="icon-plus"></i>增加参数
								</button></td>
						</tr>
					</tfoot>
				</table>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label no-padding-right" for="form-field-3"> <span style="font-size: 20px;">推送参数</span>
			</label>
			<div class="col-xs-8 col-sm-4">
				<table class="table table-striped table-bordered table-hover">
					<thead>
						<tr>
							<th>Key</th>
							<th>Value</th>
							<th></th>
						</tr>
					</thead>
					<tbody id="pushConfigs">
						<tr>
							<td><input type="text"></td>
							<td><input type="text"></td>
							<td><a class="btn btn-sm btn-danger" onclick="deleteParas(this)"><i class="icon-trash"></i>删除</a></td>
						</tr>
					</tbody>
					<tfoot>
						<tr>
							<td colspan="3"><button class="btn btn-sm btn-primary pull-right" type="button" onclick="addPushConfigs();">
									<i class="icon-plus"></i>增加参数
								</button></td>
						</tr>
					</tfoot>
				</table>
			</div>
		</div>
		<div class="col-md-offset-3 col-md-9">
			<button class="btn btn-info btn-sm" type="button" onclick="send();">
				<i class="icon-ok bigger-110"></i> 发送
			</button>
			<button class="btn btn-sm" type="reset" style="margin-left:15px;">
				<i class="icon-undo bigger-110"></i> 清空
			</button>
		</div>
	</form>
</div>
<script type="text/javascript">
	function addTemplateParas() {
		$("#templateParas")
				.append(
						'<tr><td><input type="text"></td><td><input type="text"></td><td> <a class="btn btn-sm btn-danger" onclick="deleteParas(this)"><i class="icon-trash"></i>删除</a> </td></tr>');
	}
	function addPushConfigs() {
		$("#pushConfigs")
				.append(
						'<tr><td><input type="text"></td><td><input type="text"></td><td> <a class="btn btn-sm btn-danger" onclick="deleteParas(this)"><i class="icon-trash"></i>删除</a> </td></tr>');
	}
	function deleteParas(dom) {
		$.dialog.confirm("确定删除此参数?", function() {
			$(dom).parent().parent().remove();
		});
	}

	function send() {
		var pushConfigs = {};
		var paras = {};
		$("#templateParas").children().each(function(i, item) {
			paras[$($(item).children()[0]).find("input").val()] = $($(item).children()[1]).find("input").val();
		});
		$("#pushConfigs").children().each(function(i, item) {
			pushConfigs[$($(item).children()[0]).find("input").val()] = $($(item).children()[1]).find("input").val();
		});
		$.ajax({
			url:getRootPath()+"/msg/send",
			async:false,
			type:"post",
			dataType:"json",
			data:{
				to : $("#to").val(),
				templateId:$("#templateId").val(),
				type : $("#type").val(),
				paras : JSON.stringify(paras),
				pushConfigs : JSON.stringify(pushConfigs),
				pushPlatform : $('#pushPlatform').val(),
				pushType : $('#pushType').val()
			},
			success:function(result){
				if(result.operationState ="SUCCESS"){
					r=true;
				}else{
					showMessage(result.data.reason);
					r=false;
				}
			}
		});
	}
</script>